package com.ny.zmb.openapi.jh.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mzlion.easyokhttp.HttpClient;
import com.ny.zmb.openapi.constant.JHConstant;
import com.ny.zmb.openapi.jh.JhEnum;
import com.ny.zmb.openapi.jh.UUIDUtil;
import com.ny.zmb.openapi.jh.from.JHApplyForAfterSaleFrom;
import com.ny.zmb.openapi.jh.from.JHSubOrderFrom;
import com.ny.zmb.openapi.jh.from.JHSubOrderResultFrom;
import com.ny.zmb.openapi.jh.from.JHWayBillFrom;
import com.ny.zmb.openapi.jh.vo.FareTrackVO;
import com.ny.zmb.openapi.jh.vo.FareVO;
import com.ny.zmb.openapi.jh.vo.OrderProductReqVO;
import com.ny.zmb.openapi.jh.vo.ShopAfterSaleVO;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JHOrderService {

    /*
    * 以下接口价格单位均为分
    *
    * */


    static final String TOKEN="763bf07bbc1a41aa94074bee7a921a7f";

    //查询运费
    public static JHSubOrderFrom getFreight(JHSubOrderFrom subOrderFrom){
        String content = null;
        if (null != subOrderFrom.getProdGroup() && subOrderFrom.getProdGroup().size()>0 && 0 != subOrderFrom.getRegionCode()){
            content=JSONObject.toJSONString(subOrderFrom);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.freight")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",content)
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            Object obj = result.get("data");
            String jsonObject = JSONObject.toJSONString(obj);
            BigDecimal  freight = (BigDecimal) JSONObject.parseObject(jsonObject,JSONObject.class).get("freight");
            //Double freight = (Double) jsonObject.get("freight");
            if (null != freight){
                subOrderFrom.setFreight(freight);
            }
            return subOrderFrom;
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //查询配送信息&&查询物流信息
    public static List<FareVO> getTrack(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null != orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.track")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONArray("data").toJavaList(FareVO.class);
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //查询售后服务订单
    public static List<ShopAfterSaleVO> getAfterSaleList(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null != orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",TOKEN)
                .param("method","open.api.shop.afterSale.list")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            List<ShopAfterSaleVO> shopAfterSaleVOS = result.getJSONArray("data").toJavaList(ShopAfterSaleVO.class);
           /* shopAfterSaleVOS.stream().forEach(shopAfterSaleVO->{
                Integer refundPrice = shopAfterSaleVO.getRefundPrice();
                Integer freight = shopAfterSaleVO.getFreight();
            });*/
            System.out.println(shopAfterSaleVOS);
            return shopAfterSaleVOS;
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //提交订单
    public static JHSubOrderResultFrom subOrders(JHSubOrderFrom subOrderFrom){
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.submit")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",JSONObject.toJSONString(subOrderFrom))
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data").toJavaObject(JHSubOrderResultFrom.class);
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //确认预占库存订单
    public static Boolean orderConfirm(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null!=orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.confirm")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getBoolean("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //取消未确认订单
    public static Boolean orderCancel(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null!=orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.cancel")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getBoolean("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //确认收货
    public static String orderReceived(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null!=orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.received")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getString("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //申请售后
    public static String aftersaleApply(String applyForAfterSaleFrom){
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.aftersale.apply")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",applyForAfterSaleFrom)
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getString("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    // 查询售后服务订单&&查询售后概要
    //方法不存在
    public static List<ShopAfterSaleVO> aftersaleList(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null != orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.afterSale.list")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONArray("data").toJavaList(ShopAfterSaleVO.class);
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    public static ShopAfterSaleVO aftersaleDetail(String asNo){
        JSONObject jsonObject = new JSONObject();
        if (null != asNo ){
            jsonObject.put("asNo",asNo);;
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.aftersale.detail")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data").toJavaObject(ShopAfterSaleVO.class);
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }


    // 填写运单信息&&填写发运信息
    public static Boolean aftersaleDeliver(JHWayBillFrom wayBillFrom){
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.aftersale.deliver")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",JSONObject.toJSONString(wayBillFrom))
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return true;
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    // 取消服务单&&取消售后申请
    public static Boolean aftersaleCancel(String asOrderNo){
        JSONObject jsonObject = new JSONObject();
        if (null != asOrderNo){
            jsonObject.put("asNo",asOrderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.aftersale.cancel")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return true;
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //查询推送信息
    public static Map messageList(Integer type){
        JSONObject jsonObject = new JSONObject();
        if (null != type){
            jsonObject.put("type",type);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.message.list")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data").toJavaObject(Map.class);
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }


    //删除推送信息
    public static Integer messageDelete(String ids){
        JSONObject jsonObject = new JSONObject();
        if (null != ids){
            jsonObject.put("ids",ids);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.message.delete")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getInteger("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //获取地址列表
    public static JSONArray getAddressList(Integer id){
        JSONObject jsonObject = new JSONObject();
        if (null != id){
            jsonObject.put("id",id);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.address.list")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONArray("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    //{"prodIds":"50000003,50000004,50000011"}
    public static JSONArray getProductPrice(String prodIds){
        JSONObject jsonObject = new JSONObject();
        if (null != prodIds){
            jsonObject.put("prodIds",prodIds);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.product.price")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONArray("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }

    public static JSONObject getOrderDetail(String orderNo){
        JSONObject jsonObject = new JSONObject();
        if (null != orderNo){
            jsonObject.put("orderNo",orderNo);
        }
        JSONObject result  = HttpClient.post(JHConstant.JH_URL)
                .param("appToken",JHTokenService.getAccessToken())
                .param("method","open.api.shop.order.detail")
                .param("version","2.0")
                .param("seqNo", UUIDUtil.getUUID(15))
                .param("content",jsonObject.toJSONString())
                .execute().asBean(JSONObject.class);
        result= JhEnum.get(result);
        if (0 == result.getInteger("code")){
            return result.getJSONObject("data");
        }else {
            throw new RuntimeException(result.get("msg").toString());
        }
    }


    public static void main(String[] args) {
        //查询运费
        /*OrderProductReqVO orderProductReqVO = new OrderProductReqVO();
            orderProductReqVO.setProdId(50019751);
            orderProductReqVO.setOutNum(1);
        List<OrderProductReqVO> orderProductReqVOS = new ArrayList<>();
             orderProductReqVOS.add(orderProductReqVO);

        JHSubOrderFrom jhSubOrderFrom = new JHSubOrderFrom();
        jhSubOrderFrom.setProdGroup(orderProductReqVOS);
        jhSubOrderFrom.setRegionCode(110105029);
        JHSubOrderFrom freight = getFreight(jhSubOrderFrom);
        System.out.println(freight);*/


        //查询配送信息&&查询物流信息
        List<FareVO> track = getTrack("S2024121817032291818");
        System.out.println(track);

        //查询售后服务订单
      // getAfterSaleList("S2024120615281037171");

        //提交订单
        /*String s="{\"regionCode\":110105029,\"address\":\"北京市朝阳区北苑家园茉莉园12号\",\"prodGroup\":[{\"outNum\":3,\"prodId\":50019751,\"priceOut\":126}],\"name\":\"陶先生\",\"mobile\":\"18001273521\",\"orderNoThird\":\"HC2024120614191254\"}";
        String s1 = s.replaceAll("\\\\", "");
        JSONObject jsonObject = JSONObject.parseObject(s1);
        JHSubOrderFrom jhSubOrderFrom = jsonObject.toJavaObject(JHSubOrderFrom.class);
        JHSubOrderResultFrom jhSubOrderResultFrom = subOrders(jhSubOrderFrom);
        System.out.println(jhSubOrderResultFrom);*/


        //确认预占库存订单
        //orderConfirm("S2024122314473080059");

        //取消未确认订单
       //orderCancel("S2024121811124913591");

        //确认收货
       //orderReceived("S2024122314473080059");

        //申请售后
        // {"customerAddress":"北京市朝阳区北苑家园莲葩园","orderNo":"S2024121811311625031","customerContact":"张三","pickwareType":4,"applyReason":"不好用","customerRegionCode":110105029,"prodId":50000003,"applyNum":1,"type":10,"customerTel":"18001273521"}
        // {"customerAddress":"北京市朝阳区北苑家园茉莉园12号","orderNo":"S2024121817032291818","customerContact":"陶先生","pickwareType":4,"applyReason":"不好用","customerRegionCode":110105029,"prodId":50019751,"applyNum":1,"type":10,"customerTel":"18001273521"}
        /*String s="{\"customerAddress\":\"北京市朝阳区北苑家园莲葩园\",\"orderNo\":\"S2024121816525492810\",\"customerContact\":\"陶先生\",\"pickwareType\":4,\"applyReason\":\"不好用\",\"customerRegionCode\":110105029,\"prodId\":50019751,\"applyNum\":1,\"type\":10,\"customerTel\":\"18001273521\"}";
        JSONObject jsonObject = JSONObject.parseObject(s);
        String s1 = s.replaceAll("////", "");
        JHApplyForAfterSaleFrom applyForAfterSaleFrom = jsonObject.toJavaObject(JHApplyForAfterSaleFrom.class);
        String result = aftersaleApply(s1);
        System.out.println("售后服务单号"+result);*/

        //查询售后服务订单
       /* List<ShopAfterSaleVO> shopAfterSaleVOS = aftersaleList("S2024121817032291818");
        System.out.println(shopAfterSaleVOS);*/

        //查询售后服务明细
        /*ShopAfterSaleVO shopAfterSaleVO = aftersaleDetail("A2024121909244751406");
        System.out.println(shopAfterSaleVO);*/

        //填写运单信息
        /*String s="{\"asNo\":\"A2024121909244751406\",\"deliveryDate\":\"2020-03-01\",\"carrier\":\"圆通快递\",\"deliveryNo\":\"12312312321\",\"freight\":15}";
        JSONObject jsonObject = JSONObject.parseObject(s);
        JHWayBillFrom jhWayBillFrom = jsonObject.toJavaObject(JHWayBillFrom.class);
        aftersaleDeliver(jhWayBillFrom);*/

        //取消服务单&&取消售后申请
        //aftersaleCancel("A2024120911024740277");

        //查询推送信息
       /*Map map = messageList(2);
        System.out.println(map);*/

        //删除推送信息
        /*Integer result = messageDelete("1,2,3");
        System.out.println(result);*/

        //System.out.println(JHTokenService.getAccessToken());
        //110105029    110105047

        /*JSONArray addressList = getAddressList(100000000);
        System.out.println(addressList);*/

        //查询商品价格及状态
       /* String arr="50019751,50000003,50000004,50000011";
        JSONArray productPrice = getProductPrice(arr);
        System.out.println(productPrice);*/

        //查询订单详情
        /*JSONObject orderDetail = getOrderDetail("S2024121815005766479");
        System.out.println(orderDetail);*/

    }
}
